package cn.kfm666.data.structure.sort;

import cn.kfm666.data.structure.util.GenerateData;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 冒泡排序的进阶：鸡尾酒排序
 */
public class BubbleSortPlus {
    public static void main(String[] args){
        BubbleSortPlus sortPlus = new BubbleSortPlus();
        Scanner scanner = new Scanner(System.in);
        System.out.println("please input data size:");
        int size = scanner.nextInt();
        int[] data = GenerateData.generateIntArray(size,100000);
        System.out.println("初始数据："+Arrays.toString(data));
        Long start = System.currentTimeMillis();
        sortPlus.sort(data,size);
        Long end = System.currentTimeMillis();
        System.out.println("排序数据："+Arrays.toString(data));
        System.out.println("用时："+(end-start)+"毫秒");
    }

    public void sort(int[] data,int size){
        int left = 0;
        int right = size-1;
        while (left<right){
            for(int i=left;i<size-left;i++){
                if(data[left]>data[i]){
                    int temp = data[left];
                    data[left] = data[i];
                    data[i] = temp;
                }
            }
            left++;
            for(int i=right;i>=size-right;i--){
                if(data[right]<data[i]){
                    int temp = data[right];
                    data[right] = data[i];
                    data[i] = temp;
                }
            }
            right--;
        }
    }
}
